package com.logic.landseaserver.ws;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.ws.rs.*;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;

import com.logic.common.exception.BusinessException;
import com.logic.landseaserver.common.util.DateUtil;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import com.logic.common.enums.HttpCodeEnum;
import com.logic.common.util.CommonResult;
import com.logic.common.util.CommonResultMap;
import com.logic.common.util.QueryUtil;
import com.logic.landseaserver.domain.Customer;
import com.logic.landseaserver.service.ICustomer;
import com.logic.landseaserver.ws.dto.CustomerDTO;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

/**
 * Created by Tony on 2017/4/12.
 */


@Path("/customer")
@Api(value = "customer", description = "Customer Rest WebServices", position = 42)
public class CustomerRest {

    @Autowired
    private ICustomer customerImpl;

    private Logger logger = LoggerFactory.getLogger(this.getClass());
    
    @POST
    @Path("/list")
    @Produces(MediaType.APPLICATION_JSON)
    @Consumes(MediaType.APPLICATION_JSON)
    @ApiOperation(value = "获取租客列表信息")
    public Response queryProjectAppartments(QueryUtil queryUtil) {
        CommonResultMap map = new CommonResultMap();
        List<Object> customerList = null;
        try {
            queryUtil.getPagingTool().calculateStartIndex();
            customerList = customerImpl.queryByPage(queryUtil);
            Integer totalCount = queryUtil.getPagingTool().getTotalNum();
            map.setData(customerList);
            map.setResultTotalCount(totalCount);
            map.setResult(CommonResult.SUCCESS);
        } catch (Exception e) {
            logger.error("queryProjectAppartments error.", e);
            map.setResult(CommonResult.ERROR, e.getMessage());
        }
        return Response.status(HttpCodeEnum.OK.getCode()).entity(map).build();
    }


    @POST
    @Path("/getVersionlist")
    @Produces(MediaType.APPLICATION_JSON)
    @Consumes(MediaType.APPLICATION_JSON)
    @ApiOperation(value = "获取租客列表信息")
    public Response getVersionlist(QueryUtil queryUtil) {
        CommonResultMap map = new CommonResultMap();
        List<Object> customerList = null;
        try {
            queryUtil.getPagingTool().calculateStartIndex();
            customerList = customerImpl.queryVersionPage(queryUtil);
            Integer totalCount = queryUtil.getPagingTool().getTotalNum();
            map.setData(customerList);
            map.setResultTotalCount(totalCount);
            map.setResult(CommonResult.SUCCESS);
        } catch (Exception e) {
            logger.error("queryProjectAppartments error.", e);
            map.setResult(CommonResult.ERROR, e.getMessage());
        }
        return Response.status(HttpCodeEnum.OK.getCode()).entity(map).build();
    }

    //增加租客
    @POST
    @Path("/createCustomer")
    @Produces(MediaType.APPLICATION_JSON)
    @Consumes(MediaType.APPLICATION_JSON)
    @ApiOperation(value = "创建一个租客")
    public Response createCustomer(
            @ApiParam(value = "param", required = true) CustomerDTO customer) {
        CommonResultMap map = new CommonResultMap();
        try {
            Integer id = customerImpl.createCustomer(customer);
            map.setData(id);
            map.setResult(CommonResult.SUCCESS, "z success");
        } catch (BusinessException e) {
            logger.error("createCustomer error.", e);
            map.setResult(CommonResult.BUSINESS_EXCEPTION, e.getMessage());
        } catch (Exception e) {
            logger.error("createCustomer error.", e);
            map.setResult(CommonResult.ERROR, e.getMessage());
        }
        return Response.status(HttpCodeEnum.OK.getCode()).entity(map).build();
    }

    //更新租客
    @POST
    @Path("/updateCustomer")
    @Produces(MediaType.APPLICATION_JSON)
    @Consumes(MediaType.APPLICATION_JSON)
    @ApiOperation(value = "更新一个租客")
    public Response updateCustomer(
            @ApiParam(value = "param", required = true) CustomerDTO customer) {
        CommonResultMap map = new CommonResultMap();
        try {
            Integer id = customerImpl.updateCustomer(customer);
            map.setData(id);
            map.setResult(CommonResult.SUCCESS, "success");
        } catch (Exception e) {
            logger.error("updateCustomer error.", e);
            map.setResult(CommonResult.ERROR, e.getMessage());
        }
        return Response.status(HttpCodeEnum.OK.getCode()).entity(map).build();
    }

    @POST
    @Path("/getCustomer")
    @Produces(MediaType.APPLICATION_JSON)
    @Consumes(MediaType.APPLICATION_JSON)
    @ApiOperation(value = "获取租客详情")
    public Response getCustomer(
            @ApiParam(value = "param", required = true) @QueryParam("userId") Integer id) {
        CommonResultMap map = new CommonResultMap();
        try {
            CustomerDTO customerDto = customerImpl.getCustomerInfo(id);
            map.setData(customerDto);
            map.setResult(CommonResult.SUCCESS, " success");
        } catch (Exception e) {
            logger.error("getCustomer error.", e);
            map.setResult(CommonResult.ERROR, e.getMessage());
        }
        return Response.status(HttpCodeEnum.OK.getCode()).entity(map).build();
    }

    @POST
    @Path("/queryByLoginId")
    @Produces(MediaType.APPLICATION_JSON)
    @Consumes(MediaType.APPLICATION_JSON)
    @ApiOperation(value = "获取租客详情")
    public Response queryByLoginId(
            @ApiParam(value = "param", required = true) Customer customer) {
        CommonResultMap map = new CommonResultMap();
        try {
            CustomerDTO customerDto = customerImpl.queryByLoginId(customer);
            map.setData(customerDto);
            map.setResult(CommonResult.SUCCESS, " success");
        } catch (Exception e) {
            logger.error("queryByLoginId error.", e);
            map.setResult(CommonResult.ERROR, e.getMessage());
        }
        return Response.status(HttpCodeEnum.OK.getCode()).entity(map).build();
    }

    @POST
    @Path("/selectChapterUserList")
    @Produces(MediaType.APPLICATION_JSON)
    @Consumes(MediaType.APPLICATION_JSON)
    @ApiOperation(value = "获取租客详情")
    public Response selectChapterUserList(@ApiParam(value = "param", required = true) QueryUtil queryUtil) {
        CommonResultMap map = new CommonResultMap();
        List<?> resultList = null;
        try {
            queryUtil.getPagingTool().calculateStartIndex();
            resultList = customerImpl.selectChapterUserList(queryUtil);
            Integer totalCount = queryUtil.getPagingTool().getTotalNum();
            map.setData(resultList);
            map.setResultTotalCount(totalCount);
            map.setResult(CommonResult.SUCCESS, " success");
        } catch (Exception e) {
            logger.error("selectChapterUserList error.", e);
            map.setResult(CommonResult.ERROR, e.getMessage());
        }
        return Response.status(HttpCodeEnum.OK.getCode()).entity(map).build();
    }


    @DELETE
    @Path("/deleteCustomer")
    @Produces(MediaType.APPLICATION_JSON)
    @Consumes(MediaType.APPLICATION_JSON)
    @ApiOperation(value = "删除租客")
    public Response deleteCustomer(
            @ApiParam(value = "param", required = true) @QueryParam("ids") String ids) {
        CommonResultMap map = new CommonResultMap();
        try {
            Integer id = customerImpl.deleteCustomer(ids);
            map.setData(id);
            map.setResult(CommonResult.SUCCESS, "success");
        } catch (Exception e) {
            logger.error("deleteCustomer error.", e);
            map.setResult(CommonResult.ERROR, e.getMessage());
        }
        return Response.status(HttpCodeEnum.OK.getCode()).entity(map).build();
    }

    @GET
    @Path("/getSystemDate")
    @Produces(MediaType.APPLICATION_JSON)
    @ApiOperation(value = "获取系统时间", position = 1)
    public Response getBillDetail() {
        CommonResultMap map = new CommonResultMap();
        try {
            Map<String, Object> result = new HashMap<String, Object>();
            result.put("nowDate", DateUtil.convertDateTime(new Date()));
            map.setData(result);
            map.setResult(CommonResult.SUCCESS, "getSystemDate success");
        } catch (Exception e) {
            logger.error("getBillDetail error.", e);
            map.setResult(CommonResult.ERROR, e.getMessage());
        }
        return Response.status(HttpCodeEnum.OK.getCode()).entity(map).build();
    }

    @POST
    @Path("/getChangeFlag")
    @Produces(MediaType.APPLICATION_JSON)
    @Consumes(MediaType.APPLICATION_JSON)
    @ApiOperation(value = "获取名下是否有合同和预定 0 无 1 有合同 2 有预定 3 既有合同又有预定", position = 6)
    public Response getChangeFlag(@ApiParam(value = "param", required = true) CustomerDTO customer) {
        CommonResultMap map = new CommonResultMap();
        try {
            Map<String, Object> result = new HashMap<String, Object>();
            result.put("resultFlag", customerImpl.getChangeFlag(customer));
            map.setData(result);
            map.setResult(CommonResult.SUCCESS, "getChangeFlag success");
        } catch (Exception e) {
            logger.error("getChangeFlag error.", e);
            map.setResult(CommonResult.ERROR, e.getMessage());
        }
        return Response.status(HttpCodeEnum.OK.getCode()).entity(map).build();
    }


    @POST
    @Path("/queryByUserId")
    @Produces(MediaType.APPLICATION_JSON)
    @Consumes(MediaType.APPLICATION_JSON)
    @ApiOperation(value = "根据userId获取租客详情")
    public Response queryByUserId(
            @ApiParam(value = "param", required = true) Customer customer) {
        CommonResultMap map = new CommonResultMap();
        try {
            CustomerDTO customerDto = customerImpl.queryByUserId(customer);
            map.setData(customerDto);
            map.setResult(CommonResult.SUCCESS, " success");
        } catch (Exception e) {
            logger.error("queryByUserId error.", e);
            map.setResult(CommonResult.ERROR, e.getMessage());
        }
        return Response.status(HttpCodeEnum.OK.getCode()).entity(map).build();
    }
}


